/*
 * @Author: szx
 * @Date: 2022-03-05 14:36:04
 * @LastEditTime: 2022-03-05 14:36:59
 * @Description: 
 * @FilePath: \leetcode\600-699\654\654_2.js
 */
  function TreeNode(val, left, right) {
      this.val = (val===undefined ? 0 : val)
      this.left = (left===undefined ? null : left)
     this.right = (right===undefined ? null : right)
 }

/**
 * @param {number[]} nums
 * @return {TreeNode}
 */
 var constructMaximumBinaryTree = function(nums) {
    if (nums.length == 0) return null;
    let arr = getMaxIndex(nums);
    let max = arr[0],
        index = arr[1];
    const root = new TreeNode(max);
    root.left = constructMaximumBinaryTree(nums.slice(0, index));
    root.right = constructMaximumBinaryTree(nums.slice(index + 1));
    return root;
};
const getMaxIndex = (arr) => {
    let max = arr[0];
    //声明了个变量 保存下标值
    let index = 0;
    for (let i = 0; i < arr.length; i++) {
        if (max < arr[i]) {
            max = arr[i];
            index = i;
        }
    }
    return [max, index];
};